package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result saveQ(@RequestBody SetmealDto setmealDto) {
        setmealService.saveQ(setmealDto);
        return Result.success("新增套餐成功");
    }


    @GetMapping("/page")
    public Result<Page> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                             String name) {

        //构造分页查询的条件
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        //构造需要模糊查询的条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, Setmeal::getName, name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo, wrapper);

        Page<SetmealDto> dtoPage = new Page<>();
        //封装数据total
        dtoPage.setTotal(pageInfo.getTotal());
        //封装数据records
        List<Setmeal> list = pageInfo.getRecords();
        List<SetmealDto> dtoList = list.stream().map(setmeal ->
                {
                    //把Setmeal里面的数据转移到setmealDao中
                    SetmealDto setmealDto = new SetmealDto();
                    BeanUtils.copyProperties(setmeal, setmealDto);

                    //根据id查询分类对象
                    Category category = categoryService.getById(setmeal.getCategoryId());
                    //为setmealDto中的categoryName赋值
                    if (category != null) {
                        setmealDto.setCategoryName(category.getName());
                    }

                    return setmealDto;
                }
        ).collect(Collectors.toList());

        dtoPage.setRecords(dtoList);

        return Result.success(dtoPage);
    }

    /*
     * 删除套餐
     * */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result romoveWithDish(@RequestParam List ids) {
        setmealService.removeWithDish(ids);
        return Result.success("删除成功");
    }


    @GetMapping("/{id}")
    public Result<SetmealDto> getByIdDish(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdDish(id);
        return Result.success(setmealDto);
    }


    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updates(@RequestBody SetmealDto setmealDto) {
        setmealService.updates(setmealDto);
        return Result.success("修改成功");
    }

    /**
     * 批量起售和批量停售
     *
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result haltSales(@RequestParam List ids, @PathVariable Integer status) {
        UpdateWrapper<Setmeal> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);
        wrapper.set("status", status);

        setmealService.update(wrapper);
        return Result.success();
    }


    /**
     * 根据套餐id查询套餐的基本信息
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    //@Cacheable(cacheNames = "setmealCache",key = "#setmeal.getCategoryId() +'_'+#setmeal.getStatus()")
    public Result<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(wrapper);
        return Result.success(list);

    }


}
